﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using rlTodayMakeSkin.Project;
using rlTodayMakeSkin.Project.XmlElement;
using System.Reflection;

namespace rlTodayMakeSkin.GUI
{
    /// <summary>
    /// Shows a tree rapresentation of skin element
    /// </summary>
    public class rlTodayTreeView
    {
        /***********
         * FIELDS
         ***********/
        TreeView tvToday;
        rlTodayProject project;

        /***************
         CONTRUCTORS
         ***************/
        public rlTodayTreeView(TreeView tv, rlTodayProject p)
        {
            tvToday = tv;
            project = p;
            initTreeView();
        }

        /// <summary>
        /// fill tree view with all elements in the project
        /// </summary>
        private void initTreeView()
        {
            TreeNode node = null;
            TreeNode todayNode = new TreeNode("today");
            rlTodayXmlElement xe = null;
            //first add today element
            
            for(int i=0; i < project.Elements.Count - 1; i++)
            {
                xe = project.Elements[i];
                node = new TreeNode(xe.Name);
                node.ImageKey = xe.Name + ".png";
                node.SelectedImageKey = node.ImageKey;
                addProperties(node, xe);
                todayNode.Nodes.Add(node);
            }
            tvToday.Nodes.Add(todayNode);

        }

        private void addProperties(TreeNode node, rlTodayXmlElement xe){
        //adds each property of xmlElment as a child node to this node
            Type t = null;                
            t = xe.GetType();
            TreeNode tn = null;
            Object value = null;
            foreach (PropertyInfo pi in t.GetProperties())
            {
                if (pi.CanRead == false) continue;
                value = pi.GetValue(xe, new Object[] { });
                if (value == null) continue;
                tn = node.Nodes.Add(pi.Name + " = " + value.ToString());
                node.Tag = xe;
            }

        }


    }
}
